<?php
	Header('Cache-Control: no-cache');
	Header('Pragma: no-cache');
	Header('X-UA-Compatible: IE=9');
	
	session_start();
?>
<!DOCTYPE html>
<html>
  <head>
    <title>Login</title>
    <meta name="viewport"
        content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta charset="UTF-8">
	<link rel="icon" href="icons/favicon.ico" type="image/x-icon">
    <link rel="stylesheet" type="text/css" href="css/style.css">
  </head>
  <body>
	<?php		
		// Login
		if (isset($_POST['username']) && isset($_POST['password']))
		{		
			require_once 'database/database.class.php';
			$db = new Database();
			$userid = $db->checkLogin($_POST['username'], $_POST['password']);
			if ($userid > 0)
			{
				$_SESSION['userid'] = $userid;
			}
		}
		else if (isset($_GET['logout']) && $_GET['logout'] == 1)
		{
			unset($_SESSION['userid']);
			session_destroy();
		
			require_once 'database/database.class.php';
			Database::showMsg(true, "LOGOUT");
		}
		
		require_once 'navbar.php';
		
		// Logout
		if (isset($_SESSION['userid'])) :
	?>
		<form name="logout" action="login.php">
			<input type="hidden" name="logout" value="1" />
			<div class="login">
				<input type="submit" value="Abmelden" />
			</div>
		</form>
	<?php 
		// Login
		else :
	?>
		<form name="login" action="login.php" method="post">
			<div class="phonelists">
				<table>
					<tr>
						<td class="loginLeft">Benutzername:</td>
						<td class="loginRight">
							<input type="text" name="username" 
								value="<?php if (isset($_POST['username'])){echo $_POST['username'];} ?>" />
			
						</td>
					</tr>
					<tr>
						<td class="loginLeft">Passwort:</td>
						<td class="loginRight">
							<input type="password" name="password" />
						</td>
					</tr>
				</table>
			</div>
			<div class="login">
				<input type="submit" value="Anmelden" />
			</div>
		</form>
	
	<?php endif; ?>
  </body>
</html>
